window.addEventListener('pageshow', function (e) {
    if (e.persisted) {
        window.location.reload()
    }
})
$(function () {
    sessionStorage.setItem('i', '2')
    setTimeout(function () {
        var name = localStorage.getItem('name')
        var phone = localStorage.getItem('phone')
        var ii = localStorage.getItem('diqu')
        var pp = localStorage.getItem('address')
        var userAddress = ii + pp
        if (name) {
            $('.mm').html(name)
            $('.ll').html(phone)
            // console.log( $('.gerenxinxi div').eq(1));
            $('.xuanze span').html('更换收货地址');
            $('.dizhi').html(userAddress)
        }

    }, 1000)
    var openId = sessionStorage.getItem('openId')

    var quantity = sessionStorage.getItem('quantity')

    var quantity1 = sessionStorage.getItem('quantity1')
    var quantity2 = sessionStorage.getItem('quantity2')
    var quantity3 = sessionStorage.getItem('quantity3')
    var shuliang = (Number(quantity) + Number(quantity1) + Number(quantity2) + Number(quantity3))
    var zongshu = sessionStorage.getItem('zongshu')
    console.log(quantity, quantity1, quantity2, quantity3, zongshu);
    var code = sessionStorage.getItem('code')
    console.log(456789);
    console.log(code);
    if (quantity > 0) {
        $('.bodymassage1').show()
        $('.bodymassage1 div').eq(0).html('黄油曲奇')
        $('.bodymassage1 div').eq(1).html('x' + quantity)
        $('.bodymassage1 div').eq(2).html('28.00')
        $('.xiaoji div').eq(1).html('￥' + zongshu)
        $('.jiesuan div').eq(0).html('总计￥' + zongshu)
        $('.jiesuan div').eq(1).html('微信支付￥' + zongshu)

    }
    if (quantity1 > 0) {
        $('.bodymassage2').show()
        $('.bodymassage2 div').eq(0).html('焦糖杏仁片')
        $('.bodymassage2 div').eq(1).html('x' + quantity1)
        $('.bodymassage2 div').eq(2).html('33.00')
        $('.xiaoji div').eq(1).html('￥' + zongshu)
        $('.jiesuan div').eq(0).html('总计￥' + zongshu)
        $('.jiesuan div').eq(1).html('微信支付￥' + zongshu)


    }
    if (quantity2 > 0) {
        $('.bodymassage3').show()
        $('.bodymassage3 div').eq(0).html('杏仁巧克力')
        $('.bodymassage3 div').eq(1).html('x' + quantity2)
        $('.bodymassage3 div').eq(2).html('34.00')
        $('.xiaoji div').eq(1).html('￥' + zongshu)
        $('.jiesuan div').eq(0).html('总计￥' + zongshu)
        $('.jiesuan div').eq(1).html('微信支付￥' + zongshu)

    }
    if (quantity3 > 0) {
        $('.bodymassage4').show()
        $('.bodymassage4 div').eq(0).html('玛德琳')
        $('.bodymassage4 div').eq(1).html('x' + quantity3)
        $('.bodymassage4 div').eq(2).html('25.00')
        $('.xiaoji div').eq(1).html('￥' + zongshu)
        $('.jiesuan div').eq(0).html('总计￥' + zongshu)
        $('.jiesuan div').eq(1).html('微信支付￥' + zongshu)

    }
    $('.dianji').click(function () {
        console.log(123);
        var phone = $('.address div').eq(0).find('input').val()
        console.log(phone);

        // 调起微信支付
        function onBridgeReady() {
            // var openId1 = 

            var timeStamp = sessionStorage.getItem('timeStamp')
            var nonceStr = sessionStorage.getItem('nonceStr')
            var package = sessionStorage.getItem('package')

            var signType = sessionStorage.getItem('signType')
            var paySign = sessionStorage.getItem('paySign')
            var oppId = sessionStorage.getItem('oppId')
            var timo = {
                appId: oppId,
                timeStamp: timeStamp,
                nonceStr: nonceStr,
                package: package,
                signType: signType,
                paySign: paySign
            }

            WeixinJSBridge.invoke(
                // 'getBrandWCPayRequest', {
                //     "appId": "wx2421b1c4370ec43b", //公众号ID，由商户传入     
                //     "timeStamp": "1395712654", //时间戳，自1970年以来的秒数     
                //     "nonceStr": "e61463f8efa94090b1f366cccfbbb444", //随机串     
                //     "package": "prepay_id=u802345jgfjsdfgsdg888",
                //     "signType": "MD5", //微信签名方式：     
                //     "paySign": "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
                // },
                'getBrandWCPayRequest', timo,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        // 使用以上方式判断前端返回,微信团队郑重提示：
                        //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                        window.location.href = 'index.html'

                    }
                });
        }

        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }

    })

    var name = localStorage.getItem('name')
    var phone = localStorage.getItem('phone')
    var userAddress = localStorage.getItem('diqu') + localStorage.getItem('address')
    var qu = localStorage.getItem('diqu')
    var dizhi = localStorage.getItem('address')
    if (name) {
        placeAnOrder()
    }

    function placeAnOrder() {

        var goodsId = sessionStorage.getItem('goodsId1') + ',' + sessionStorage.getItem('goodsId2') + ',' + sessionStorage.getItem('goodsId3') + ',' + sessionStorage.getItem('goodsId4')
        var data = {
            openId: openId,
            goodsId: goodsId,
            orderNum: shuliang,
            payBala: zongshu,
            userName: name,
            userPhone: phone,
            userAddress: userAddress

        }

        $.ajax({
            url: 'https://cakewxbk.haguodutimes.com/handler/order',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            async: true, //异步请求
            cache: false,
            data: JSON.stringify(data),
            success: function (json) {
                sessionStorage.setItem('timeStamp', json.obj.timeStamp)
                sessionStorage.setItem('nonceStr', json.obj.nonceStr)
                sessionStorage.setItem('package', json.obj.packageInfo)
                sessionStorage.setItem('signType', json.obj.signType)
                sessionStorage.setItem('paySign', json.obj.paySign)
                sessionStorage.setItem('oppId', json.obj.appid)

            }
        })
    }
    $('.xuanze').click(function () {
        $('.mask').show();
        $('.dibuAddtess').show();
        $('.fotter').show();
        if (name) {
            $('.a1').val(name);
            $('.a2').val(phone);

            $('.a3').val(qu);
            $('.a4').val(dizhi);
        }



    })

    $('.fotter').on('click', function (e) {
        console.log(123);
        var name = $('.address div').eq(0).find('input').val().trim();
        var phone = $('.address div').eq(1).find('input').val().trim();
        var diqu = $('.address div').eq(2).find('input').val().trim();
        var address = $('.address div').eq(3).find('input').val().trim().replace(/[\r\n]/g, "");
        if (name == '') {
            alert('请填写收货人')
        } else if (phone == '' || phone.length != 11) {
            alert('请检查手机号')
        } else if (diqu == '' || address == '') {
            alert('地址信息请补充完整')
        } else {
            var position = diqu + '，' + address
            $('.dizhi').html(position)
            $('.mm').html(name)
            $('.ll').html(phone)
            localStorage.setItem('name', name)
            localStorage.setItem('phone', phone)
            localStorage.setItem('diqu', diqu)
            localStorage.setItem('address', address)
            $('.mask').hide();
            $('.fotter').hide();
            $('.dibuAddtess ').hide();
            $('.xuanze span').html('更换收货地址');
        }

    })

    $('.mask').click(function (event) {
        event.target == this
        event.stopPropagation();
        event.preventDefault();
        console.log(456);
        $('.mask').hide();
        $('.dibuAddtess').hide();

    })

})